package com.liucc.gulimall.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 处理跨域请求配置类
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter(){

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedHeader("*"); // 允许任意请求头跨域
        corsConfiguration.addAllowedMethod("*");    // 允许任意请求方式跨域
        corsConfiguration.addAllowedOriginPattern("*");    // 配置任意域名
        corsConfiguration.setAllowCredentials(true); // 允许携带cookie
        source.registerCorsConfiguration("/**", corsConfiguration); // 对所有请求进行拦截，配置跨域策略
        return new CorsWebFilter(source);
    }
}
